package cn.dida.dao;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.dida.model.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import cn.dida.model.LgsBillsProduct;
import cn.dida.util.DateUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>扫码管理 数据交互方法</p>
 * @auther lisha
 * @date 2017-2-9 10:35:15
 */
@Repository
public class LgsBillsDao extends DaoBase {
	
	/**
	 * 创建单据
	 * @param lgsBills 单据实体类
	 * @return 返回受影响的记录行数
	 * @date 2017-2-9 10:44
	 */
	@Transactional
	public int insertLgsBills (LgsBills lgsBills) {
		
		 String sql = "insert into lgs_bills(bills_no, node_id,depot_id_from,depot_id_to,imgofvoucher, remark, type, sta, "
		 		+ "c_time, c_admin,company_id) values (:billsNo, :nodeId, :depotIdFrom, :depotIdTo, :imgofvoucher, :remark, :type, :sta, :cTime, "
		 		+ ":cAdmin, :companyId)";
		 
		Date currentDate = new Date();
		lgsBills.setcTime(currentDate.getTime());
		lgsBills.setSta(1);//状态，1初始创建，2使用中，3完成，4封存
		lgsBills.setType(1);//类型，1自建，2erp抓取，3其他

		//创建单据
		KeyHolder keyHolder=new GeneratedKeyHolder();
		SqlParameterSource ps=new BeanPropertySqlParameterSource(lgsBills);
		namedParameterJdbcTemplate.update(sql, ps, keyHolder);
		//新创建的单据id
		int billId = keyHolder.getKey().intValue();
	        
		//创建 单据与产品关联
		LgsBillsPro pros[] = lgsBills.getLgsBillsPro();
		if(pros.length>0){
			for(int i = 0; i < pros.length; i++){
				LgsBillsProduct billsProduct = new LgsBillsProduct();
				billsProduct.setLgsBillsId(billId);
				billsProduct.setProductId(Integer.valueOf(pros[i].getProId()));
				billsProduct.setProductName(pros[i].getProName());
				billsProduct.setNubs(pros[i].getProNub());
				insertBillsProduct(billsProduct);

			}
		}
		return 1;
	}
	/**
	 * 修改单据
	 * @param lgsBills 单据实体类
	 * @return 返回受影响的记录行数
	 * @date 2017-2-14 15:04
	 */
	public int updateBills (LgsBills lgsBills) {

		String sql = "update lgs_bills set bills_no=:billsNo,node_id=:nodeId,depot_id_from=:depotIdFrom,depot_id_to=:depotIdTo,"
        		+ " imgofvoucher=:imgofvoucher,remark=:remark"
        		+ " where id = :id and company_id=:companyId";
		
		HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("billsNo", lgsBills.getBillsNo());
        paramMap.put("nodeId", lgsBills.getNodeId());
        paramMap.put("depotIdFrom", lgsBills.getDepotIdFrom());
        paramMap.put("depotIdTo", lgsBills.getDepotIdTo());
        paramMap.put("imgofvoucher", lgsBills.getImgofvoucher());
        paramMap.put("remark", lgsBills.getRemark());
        paramMap.put("id", lgsBills.getId());
        paramMap.put("companyId", lgsBills.getCompanyId());
		//修改单据
        namedParameterJdbcTemplate.update(sql, paramMap);
		
        //删除已存在
		deleteBiProByBillid(lgsBills.getId());

		//创建 单据与产品关联
		LgsBillsPro pros[] = lgsBills.getLgsBillsPro();
		if(pros.length>0){
			for(int i = 0; i < pros.length; i++){
				LgsBillsProduct billsProduct = new LgsBillsProduct();
				billsProduct.setLgsBillsId(lgsBills.getId());
				billsProduct.setProductId(Integer.valueOf(pros[i].getProId()));
				billsProduct.setProductName(pros[i].getProName());
				billsProduct.setNubs(Integer.valueOf(pros[i].getProNub()));
				insertBillsProduct(billsProduct);
			}
		}
		return 1;
	}

	/**
	 * 创建单据与产品关联信息
	 * @param billsProduct 
	 * @return 受影响的记录行数
	 * @date 2017-2-9 11:01
	 */
	private int insertBillsProduct (LgsBillsProduct billsProduct) {
		String sql = "insert into lgs_bills_product(lgs_bills_id, product_id,product_name,nubs) values (:billsId, :productId, :productName, :nubs)";
		HashMap<String, Object> paramMap = new HashMap<>(); 
		paramMap.put("billsId", billsProduct.getLgsBillsId());
		paramMap.put("productId", billsProduct.getProductId());
		paramMap.put("productName", billsProduct.getProductName());
		paramMap.put("nubs", billsProduct.getNubs());
		return namedParameterJdbcTemplate.update(sql, paramMap);
	}
	/**
	 * 根据关联id删除单据与产品关联信息
	 * @param id 单据与商品关联表id
	 * @return int 受影响的记录行数
	 * @date 2017-2-14 15:21
	 */
	private int deleteBillsProduct(int id){
		String sql = "delete from lgs_bills_product where id = ?";
		return jdbcTemplate.update(sql,id);
	}

	/**
	 * 根据单据id删除单据与产品关联信息
	 * @param billId 单据与商品关联表id
	 * @return int 受影响的记录行数
	 * @date 2017-2-14 15:21
	 */
	private int deleteBiProByBillid(int billId){
		String sql = "delete from lgs_bills_product where lgs_bills_id = ?";
		return jdbcTemplate.update(sql,billId);
	}
	
	
	/**
	 * 单据列表
	 * @param companyId 公司id
	 * @param productName 产品名称
	 * @param billsNo 单据号
	 * @param billsType 单据类型
	 * @param node 节点
	 * @param nubsS 应扫数量（小）
	 * @param nubsE 应扫数量（大）
	 * @param from 发货地点
	 * @param to 收货地点
	 * @param cTimeS 操作时间（开始）
	 * @param cTimeE 操作时间 （结束）
	 * @param sta 状态，1初始创建，2使用中，3完成，4封存
	 * @param page 分页游标
	 * @return Pageable<LgsBills>单据分页列表
	 * @date 2017-2-9 11:52
	 */
	public Pageable<LgsBills> query (Integer companyId, String productName, String billsNo, Integer billsType, Integer node, Integer nubsS, Integer nubsE,
									 Integer scanNubsS, Integer scanNubsE,Integer from, Integer to,Integer depotScan, String cTimeS, String cTimeE, Integer sta, int page) {
		Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from lgs_bills b "
        		+ "join lgs_node n on b.node_id = n.id where b.company_id = ?");
        StringBuffer sqlQueryResult = new StringBuffer("select b.*,  "
        		+ "n.name 'nodeName', "
        		+ "(select d.cust_name from lgs_bd_cust d where d.id = b.depot_id_from) as 'from',"
        		+ "(select d.cust_name from lgs_bd_cust d where d.id = b.depot_id_to) as 'to',"
				+ "de.name as 'depotScanName'"
        		+ " from lgs_bills b join lgs_node n on b.node_id = n.id left join lgs_depot de on de.id=b.depot_scan"
        		+ " where b.company_id = ?");

        args = ArrayUtils.add(args, companyId);
        StringBuffer sqlWhere = new StringBuffer() ;
        
        if(StringUtils.isNotBlank(productName) || nubsS != null || nubsE != null|| scanNubsS!=null || scanNubsE!=null) {//产品名称
			StringBuffer strWhere = new StringBuffer() ;
			strWhere.append(" and b.ID in ( select DISTINCT  lgs_bills_id from lgs_bills_product where 1=1 ");
			if(StringUtils.isNotBlank(productName)){
				strWhere.append(" and product_name like ?");
				args = ArrayUtils.add(args, "%" + productName + "%");
			}
			if(nubsS != null){
				strWhere.append(" and nubs >= ?");
        	    args = ArrayUtils.add(args, nubsS);
			}

			if(nubsE != null){
				strWhere.append(" and nubs <= ?");
       			args = ArrayUtils.add(args, nubsE);
			}
			if(scanNubsS != null){
				strWhere.append(" and scan_nubs >= ?");
				args = ArrayUtils.add(args, scanNubsS);
			}

			if(scanNubsE != null){
				strWhere.append(" and scan_nubs <= ?");
				args = ArrayUtils.add(args, scanNubsE);
			}
			strWhere.append(")");
			sqlWhere.append(strWhere);

        }
        if(StringUtils.isNotBlank(billsNo)){//单据号
        	sqlWhere.append(" and b.bills_no = ?");
        	args = ArrayUtils.add(args, billsNo);
        }
        if(billsType != null){//类型，1自建，2erp抓取，3其他
        	sqlWhere.append(" and b.type = ?");
        	args = ArrayUtils.add(args, billsType);
        }
        if(node != null){//节点
        	sqlWhere.append(" and b.node_id = ?");
        	args = ArrayUtils.add(args, node);
        }

        if(from != null){//发货仓库
        	sqlWhere.append(" and b.depot_id_from = ?");
        	args = ArrayUtils.add(args, from);
        }
        if(to != null){//收货仓库
        	sqlWhere.append(" and b.depot_id_to = ?");
        	args = ArrayUtils.add(args, to);
        }
		if(depotScan != null){//扫描仓库
			sqlWhere.append(" and b.depot_scan = ?");
			args = ArrayUtils.add(args, depotScan);
		}
        if(StringUtils.isNotBlank(cTimeS)){//操作时间（开始）
        	sqlWhere.append(" and b.c_time >= ?");
        	try {
				cTimeS = String.valueOf(DateUtils.stringToLong(cTimeS, "yyyy-MM-dd HH:mm:ss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
        	long ctimes = Long.parseLong(cTimeS);
        	args = ArrayUtils.add(args, ctimes);
        }
        if(StringUtils.isNotBlank(cTimeE)){//操作时间（结束）
        	sqlWhere.append(" and b.c_time <= ?");
        	try {
        		cTimeE = String.valueOf(DateUtils.stringToLong(cTimeE, "yyyy-MM-dd HH:mm:ss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
        	long ctimee = Long.parseLong(cTimeE);
        	args = ArrayUtils.add(args, ctimee);
        }
        if(sta != null){//状态，1初始创建，2使用中，3完成，4封存
        	sqlWhere.append(" and b.sta = ?");
        	args = ArrayUtils.add(args, sta);
        }
        
        Integer resultTotal = 0;
        // 查询记录总数
        if(sqlWhere != null){
        	resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        	// 查询记录
        	sqlQueryResult.append(sqlWhere).append("  order by c_time desc limit ?,?");
        }else{
        	resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.toString(), Integer.class, args);
        	sqlQueryResult.append("  order by c_time desc limit ?,?");
        }
        
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<LgsBills> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(LgsBills.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
	}


	/**
	 * 单据列表
	 * @param companyId 公司id
	 * @param productName 产品名称
	 * @param billsNo 单据号
	 * @param billsType 单据类型
	 * @param node 节点
	 * @param nubsS 应扫数量（小）
	 * @param nubsE 应扫数量（大）
	 * @param from 发货地点
	 * @param to 收货地点
	 * @param cTimeS 操作时间（开始）
	 * @param cTimeE 操作时间 （结束）
	 * @param sta 状态，1初始创建，2使用中，3完成，4封存
	 * @return Pageable<LgsBills>单据分页列表
	 * @date 2017-2-9 11:52
	 */
	public List<LgsBills> queryForExport (Integer companyId, String productName, String billsNo, Integer billsType, Integer node,
										  Integer scanNubsS, Integer scanNubsE,Integer nubsS, Integer nubsE,
									 Integer from, Integer to,Integer depotScan, String cTimeS, String cTimeE, Integer sta) {
		Object[] args = {};
		StringBuffer sqlQueryResult = new StringBuffer("select b.*,  "
				+ "n.name 'nodeName', "
				+ "(select d.cust_name from lgs_bd_cust d where d.id = b.depot_id_from) as 'from',"
				+ "(select d.cust_name from lgs_bd_cust d where d.id = b.depot_id_to) as 'to',"
				+ "de.name as 'depotScanName'"
				+ " from lgs_bills b join lgs_node n on b.node_id = n.id left join lgs_depot de on de.id=b.depot_scan"
				+ " where b.company_id = ?");

		args = ArrayUtils.add(args, companyId);
		StringBuffer sqlWhere = new StringBuffer() ;

		if(StringUtils.isNotBlank(productName) || nubsS!=null || nubsE!=null || scanNubsS!=null || scanNubsE!=null) {//产品名称
			StringBuffer strWhere = new StringBuffer() ;
			strWhere.append(" and b.ID in ( select DISTINCT  lgs_bills_id from lgs_bills_product where 1=1 ");
			if(StringUtils.isNotBlank(productName)){
				strWhere.append(" and product_name like ?");
				args = ArrayUtils.add(args, "%" + productName + "%");
			}
			if(nubsS != null){
				strWhere.append(" and nubs >= ?");
				args = ArrayUtils.add(args, nubsS);
			}

			if(nubsE != null){
				strWhere.append(" and nubs <= ?");
				args = ArrayUtils.add(args, nubsE);
			}
			if(scanNubsS != null){
				strWhere.append(" and scan_nubs >= ?");
				args = ArrayUtils.add(args, scanNubsS);
			}

			if(scanNubsE != null){
				strWhere.append(" and scan_nubs <= ?");
				args = ArrayUtils.add(args, scanNubsE);
			}
			strWhere.append(")");
			sqlWhere.append(strWhere);

		}
		if(StringUtils.isNotBlank(billsNo)){//单据号
			sqlWhere.append(" and b.bills_no = ?");
			args = ArrayUtils.add(args, billsNo);
		}
		if(billsType != null){//类型，1自建，2erp抓取，3其他
			sqlWhere.append(" and b.type = ?");
			args = ArrayUtils.add(args, billsType);
		}
		if(node != null){//节点
			sqlWhere.append(" and b.node_id = ?");
			args = ArrayUtils.add(args, node);
		}
		if(from != null){//发货仓库
			sqlWhere.append(" and b.depot_id_from = ?");
			args = ArrayUtils.add(args, from);
		}
		if(to != null){//收货仓库
			sqlWhere.append(" and b.depot_id_to = ?");
			args = ArrayUtils.add(args, to);
		}
		if(depotScan != null){//扫描仓库
			sqlWhere.append(" and b.depot_scan = ?");
			args = ArrayUtils.add(args, from);
		}
		if(StringUtils.isNotBlank(cTimeS)){//操作时间（开始）
			sqlWhere.append(" and b.c_time >= ?");
			try {
				cTimeS = String.valueOf(DateUtils.stringToLong(cTimeS, "yyyy-MM-dd HH:mm:ss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			long ctimes = Long.parseLong(cTimeS);
			args = ArrayUtils.add(args, ctimes);
		}
		if(StringUtils.isNotBlank(cTimeE)){//操作时间（结束）
			sqlWhere.append(" and b.c_time <= ?");
			try {
				cTimeE = String.valueOf(DateUtils.stringToLong(cTimeE, "yyyy-MM-dd HH:mm:ss"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			long ctimee = Long.parseLong(cTimeE);
			args = ArrayUtils.add(args, ctimee);
		}
		if(sta != null){//状态，1初始创建，2使用中，3完成，4封存
			sqlWhere.append(" and b.sta = ?");
			args = ArrayUtils.add(args, sta);
		}

		if(sqlWhere != null){
			// 查询记录
			sqlQueryResult.append(sqlWhere).append("  order by c_time desc ");
		}else{
			sqlQueryResult.append("  order by c_time desc");
		}

		List<LgsBills> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(LgsBills.class));
		return result;
	}





	/**
	/**
	 * 根据id查询单个单据
	 * @param id 单据id
	 * @param companyId 公司id
	 * @return 单个节点
	 * @date 2017-2-9 13:50
	 */
	public LgsBills getLgsBillById (int id, int companyId) {
		String sql = "select b.*, n.name 'nodeName', n.type 'nodeType', cf.cust_name 'from',cf.cust_code 'fromCode', ct.cust_name  'to',ct.cust_code 'toCode'"
				+ " from lgs_bills b left join lgs_node n on b.node_id = n.id "
				+ "left join lgs_bd_cust cf on cf.id=b.depot_id_from left join lgs_bd_cust ct on ct.id = b.depot_id_to"
				+ " where b.id = ? and b.company_id = ?";
		List<LgsBills> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LgsBills.class), id, companyId);
		if (result.size() > 0) {
			return result.get(0);
		}else{
			return new LgsBills();
		}
	}
	/**
	 * 瓶数、箱数合计
	 * @param billId 单据id
	 * @return
	 * @date 2017-2-14 13:33
	 */
	public LgsBillsProduct getTotalNubsByBillId (int billId) {
		String sql = "select sum(bp.nubs) AS 'totalNubs', sum((ceil(bp.nubs*pb.pcount))) as 'totalBox' "
				+ "from lgs_bills_product bp left join product p on bp.product_id = p.id "
				+ "left join pbc pb on p.pbc_id = pb.id where bp.lgs_bills_id = ?";
		List<LgsBillsProduct> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LgsBillsProduct.class),billId);
		if(result.size() > 0){
			return result.get(0);
		}else{
			return new LgsBillsProduct();
		}
	}
	/**
	 * 根据单据id获取关联的产品
	 * @param billId 单据id
	 * @return List<LgsBillsProduct> 单据关联的产品列表
	 * @date 2017-2-9 13:54
	 */
	public List<LgsBillsProduct> getProductByBillId (int billId) {
		String sql = "select bp.lgs_bills_id 'lgsBillsId', bp.product_id 'productId', bp.nubs , bp.scan_nubs 'scanNubs', p.name 'productName', p.number 'number', p.barcode 'barcode', pb.pcount 'pbc', (ceil(bp.nubs*pb.pcount)) as 'nubsFox' "
				+ "from lgs_bills_product bp left join product p on bp.product_id = p.id left join pbc pb on p.pbc_id = pb.id where lgs_bills_id = ?";
		List<LgsBillsProduct> productList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LgsBillsProduct.class), billId);
		return productList;
	}
	
	/**
	 * 判断单据是否已存在
	 * @param companyId 公司id
	 * @param billNo 单据号
	 * @return boolean true存在，false不存在
	 * @date 2017-2-9 14:13
	 */
    public boolean isExistsBillNo(Integer companyId, String billNo, Integer type){
    	String sql = "select * from lgs_bills where company_id = ? and bills_no = ?  and type = ?";
    	List<LgsBills> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LgsBills.class),companyId,billNo,type);
    	if(result.size() > 0){
    		return true;
    	}else{
    		return false;
    	}
    }
    /**
     * 作废单据
     * @param companyId 公司id
     * @param id 单据id
     * @return boolean true操作成功，false操作失败
     * @date 2017-2-9 14:19
     */
    public boolean billInvalid(Integer companyId, int id) {//状态，1初始创建，2使用中，3完成，4封存 ,5已作废
    	String sql = "update lgs_bills set sta = 5 where company_id = ? and id = ? ";
    	int i = jdbcTemplate.update(sql, companyId, id);
    	if(i > 0){
    		return true;
    	}else {
    		return false;
    	}
    }


	/**
	 * 根据公司id 获取公司erp_code
	 * @param companyId
	 * @return
	 */
	public String getComanyErpCode(Integer companyId){
		String sql="select erp_code from company where id=?";
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sql,companyId);
		if(result.size() > 0){
			return (String)result.get(0).get("erp_code");
		}else{
			return null;
		}
	}
    /**
     * 获取地点列表
     * @param companyId 公司id
     * @param sta 是否可用，1可用，0屏蔽
     * @return List<LgsDepot> 地点列表
     * @date 2017-02-13 15:17
     */
    public List<LgsDepot> getDepotList(Integer companyId, Integer sta){
    	String sql="SELECT * from lgs_depot d WHERE d.sta = ? and d.company_id = ? ";
    	return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(LgsDepot.class), sta, companyId);
    }
    
    /**
     * 扫码管理-> 明细列表
     * @param billId 单据id
     * @param companyId 公司id
     * @param batchNub 批次号
     * @param qrcodeBoxId 箱码
     * @param qrcodeBottleId 瓶码
     * @param page 分页游标
     * @return Pageable<LgsLogPdaScan> 明细分页列表
     * @date 2017-2-14 17:10
     */
    public Pageable<LgsLogPdaScan> queryScanDetail (String billId, Integer companyId, String batchNub, String qrcodeBoxId, String qrcodeBottleId, int page) {
		Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from lgs_log_pda_scan sc where sc.bills_id = ? and sc.company_id = ?");
        StringBuffer sqlQueryResult = new StringBuffer("select sc.*,(select p.name from product p where p.id = sc.product_id) as 'productName',  "
        		+ " (select name from lgs_depot where id = sc.depot_id) as 'depotName',"
        		+ " (select type from lgs_node where id = b.node_id) as 'nodeType',"
        		+ "(select cust_name from lgs_bd_cust where id = b.depot_id_from ) as 'fromAddress',"
        		+ "(select cust_name from lgs_bd_cust where id = b.depot_id_to ) as 'toAddress', "
        		+ "(select u.real_name from lgs_pda_user u  where u.id = sc.pda_user_id ) as 'pdaUserName' "
        		+ " from lgs_log_pda_scan sc JOIN lgs_bills b ON sc.bills_id = b.ID where sc.bills_id = ? and sc.scan_company_id = ? ");

        args = ArrayUtils.add(args, billId);
        args = ArrayUtils.add(args, companyId);
        StringBuffer sqlWhere = new StringBuffer() ;
        
        if(StringUtils.isNotBlank(batchNub)){
        	sqlWhere.append(" and sc.batch_nub = ?");
        	args = ArrayUtils.add(args, batchNub);
        }
        if(StringUtils.isNotBlank(qrcodeBoxId )){
        	sqlWhere.append(" and sc.qrcode_box_id = ?");
        	args = ArrayUtils.add(args, qrcodeBoxId);
        }
        if(StringUtils.isNotBlank(qrcodeBottleId)){
        	sqlWhere.append(" and sc.qrcode_box_id in (select b_qrcode_id from res_qrbind where p_qrcode_id = ? and contact_type =1 and company_id =? and dr=0)");
        	args = ArrayUtils.add(args, qrcodeBottleId);
			args = ArrayUtils.add(args, companyId);
        }
        
        Integer resultTotal = 0;
        // 查询记录总数
        if(sqlWhere != null){
        	resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        	// 查询记录
        	sqlQueryResult.append(sqlWhere).append("  order by sc.scan_time desc limit ?,?");
        }else{
        	resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.toString(), Integer.class, args);
        	sqlQueryResult.append("  order by sc.scan_time desc limit ?,?");
        }
        
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<LgsLogPdaScan> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(LgsLogPdaScan.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
	}
}
